<?php

/* ::: USER CLASS :::*/
/* Creates a user */
class user
{
    private $username;
    private $userid;
    private $db;
    private $usertable;
    private $userIdColum;
    private $points;

    function __construct( array $args )
    {
        foreach ( $args as $att => $val )
        {
            if ( is_null( $this->$att ) )
            {
                $this->$att = $val;
            }
        }
        
        if(empty($this->userid)) {
            $this->generateUserId();
        }
    }

    //Sets the username
    function setUsername( string $username )
    {
        $this->username = $username;
    }

    //Returns username
    function getUsername()
    {
        return $this->username;
    }

    //Returuns userid
    function getUserID()
    {
        return $this->userid;
    }

    //Sets userid
    function setUserID( int $userid )
    {
        $this->userid = $userid;
    }

    //Sets userpoints
    function setPoints( int $points )
    {
        $this->points = $points;
    }

    //Get points
    function getPoints()
    {
        return $this->points;
    }

    //Generate a userid an returns it
    function generateUserId()
    {
        if ( ! empty( $this->usertable ) && ! empty( $this->userIdColum ) && is_object( $this->db ) )
        {
            $table = $this->$db->getTable();
            $this->$db->connect( false );

            for ( ;; )
            {
                $genUserId = rand( 0000, 9999 );
                $data = $db->get( "SELECT * FROM $table WHERE userid='$genUserId'" );

                if ( ! is_array( $data ) )
                {
                    return $genUserId;
                }
            }
            $db->disconnect();
        }
    }
    
    
}
